import sys
import os
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QLineEdit, QTextEdit, QPushButton, QFileDialog, QMessageBox, QScrollArea, QFrame, QFormLayout, QGroupBox, QHBoxLayout
from PyQt5.QtCore import Qt, QDateTime
from PyQt5.QtGui import QFont, QColor, QPalette, QPixmap

class SurveyApp(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle("flow 测试")
        self.setGeometry(100, 100, 600, 600)

        # 设置样式表
        self.setStyleSheet("""
            QWidget {
                background-color: #f8f9fa;
                font-family: Arial, sans-serif;
            }
            QLabel {
                color: #333333;
                font-size: 14px;
            }
            QLineEdit, QTextEdit {
                padding: 10px;
                border: 1px solid #cccccc;
                border-radius: 4px;
                background-color: #ffffff;
            }
            QPushButton {
                background-color: #4CAF50;
                color: white;
                padding: 10px 20px;
                border: none;
                border-radius: 4px;
                font-size: 14px;
            }
            QPushButton:hover {
                background-color: #45a049;
            }
            QFrame, QGroupBox {
                background-color: #ffffff;
                border: 1px solid #cccccc;
                border-radius: 4px;
                margin-bottom: 10px;
                padding: 10px;
            }
        """)

        layout = QVBoxLayout()

        # 创建一个滚动区域
        scroll_area = QScrollArea()
        scroll_area.setWidgetResizable(True)
        scroll_content = QWidget()
        scroll_layout = QVBoxLayout(scroll_content)
        scroll_area.setWidget(scroll_content)

        # 问题列表
        blank_questions = [
            "答题时间：",
            "昨晚睡眠时间：",
            "早上起床时间：",
            "睡眠时间：",
            "上午工作时间：",
            "下午工作时间：",
            "晚上工作时间：",
            "总工作时间：",
            "上午的工作时间状态：",
            "下午的工作时间状态：",
            "晚上的工作时间状态：",
            "一天的三餐状态：",
            "三餐的花销：",
            "觉得不快乐的地方："
        ]

        self.answers = []
        self.all_questions = []

        # 添加填空题
        for question in blank_questions:
            blank_frame = QFrame()
            blank_layout = QFormLayout()
            blank_label = QLabel(question)
            if question in ["觉得不快乐的地方：", "一天的三餐状态：", "上午的工作时间状态：", "下午的工作时间状态：", "晚上的工作时间状态："]:
                blank_entry = QTextEdit()
                blank_entry.setFixedHeight(100)  # 设置固定高度
            else:
                blank_entry = QLineEdit()
            blank_layout.addRow(blank_label, blank_entry)
            blank_frame.setLayout(blank_layout)
            scroll_layout.addWidget(blank_frame)

            self.answers.append(blank_entry)
            self.all_questions.append(question)

        # 添加保存按钮
        save_button = QPushButton("保存答案")
        save_button.clicked.connect(self.save_answers)
        layout.addWidget(save_button)

        layout.addWidget(scroll_area)
        self.setLayout(layout)

    def get_custom_save_path(self):
        custom_path = "D:\\desktop\\windows\\flow"
        if not os.path.exists(custom_path):
            os.makedirs(custom_path)
        return custom_path

    def save_answers(self):
        custom_path = self.get_custom_save_path()
        current_time = QDateTime.currentDateTime().toString("yyyyMMdd_HHmmss")
        file_path = os.path.join(custom_path, f"answers_{current_time}.txt")

        with open(file_path, "w", encoding="utf-8") as file:
            for i, answer in enumerate(self.answers):
                question = self.all_questions[i]
                if isinstance(answer, QLineEdit):
                    file.write(f"{question} {answer.text()}\n")
                elif isinstance(answer, QTextEdit):
                    file.write(f"{question} {answer.toPlainText()}\n")

        QMessageBox.information(self, "保存成功", f"答案已保存到 {file_path}")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = SurveyApp()
    ex.show()
    sys.exit(app.exec_())